﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCEdu;
using Utils;


public partial class Search_Tutor : System.Web.UI.Page
{
    DataSet dataSet;

    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.AllowPaging = true;
        GridView1.AllowSorting = true;
        GridView1.EmptyDataText = "Không có dữ liệu phù hợp";
        GridView1.PageSize = 10;

        if (!IsPostBack)
        {
            BindDataGridView();
        }          
    }

    protected void BindDataGridView()
    {
        if (dataSet == null)
        {
            SqlConnection sqlCon = null;
            try
            {

                sqlCon = new SqlConnection(Common.Conn);
                SqlCommand cmd = new SqlCommand("TutorProfile_Search", sqlCon);
                cmd.CommandType = CommandType.StoredProcedure;
                sqlCon.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                dataSet = new DataSet();
                adapter.Fill(dataSet);

                GridView1.DataSource = dataSet;
                GridView1.DataBind();

                cboName.DataSource = dataSet.Tables[0].DefaultView.ToTable(true,"Name");
                cboEducation.DataSource = dataSet.Tables[0].DefaultView.ToTable(true, "Education");
                cboEmail.DataSource = dataSet.Tables[0].DefaultView.ToTable(true, "Email");
                cboUniversity.DataSource = dataSet.Tables[0].DefaultView.ToTable(true, "University");
                cboSpecialize.DataSource = dataSet.Tables[0].DefaultView.ToTable(true, "Specialize");

                cboName.DataBind();
                cboEducation.DataBind();
                cboUniversity.DataBind();
                cboEmail.DataBind();
                cboSpecialize.DataBind();

            }
            catch (Exception ex)
            {
                MyMessage.ShowMessage(ex.ToString());
            }
            finally
            {
                if (sqlCon != null) sqlCon.Close();
            }
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection sqlCon = null;

        try
        {
            sqlCon = new SqlConnection(Common.Conn);
            SqlCommand cmd = new SqlCommand("TutorProfile_Search", sqlCon);
            cmd.CommandType = CommandType.StoredProcedure;
            
            cmd.Parameters.Add(new SqlParameter("@Name", cboName.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Specialize", cboSpecialize.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Gender", (ddlGender.SelectedValue.Trim() == "0"?false:true)));
            cmd.Parameters.Add(new SqlParameter("@Email", cboEmail.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@University", cboUniversity.SelectedText.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Subjects", tbxSubjects.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Salary_Start", tbxSalary_Start.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Salary_End", tbxSalary_End.Text.Trim()));
            cmd.Parameters.Add(new SqlParameter("@Education", cboEducation.SelectedText.Trim()));
            sqlCon.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            dataSet = new DataSet();
            adapter.Fill(dataSet);

            GridView1.DataSource = dataSet;
            GridView1.DataBind();

        }
        catch (Exception ex)
        {
            MyMessage.ShowMessage(ex.ToString());
        }
        finally
        {
            if (sqlCon != null) sqlCon.Close();
        }

    }

    protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        string strFileName = "Danh%Sach%Gia%Su.xls";
        
        DataGrid dg = new DataGrid();
        dg.DataSource = GridView1.DataSource;
        dg.DataBind();        
        
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
        Response.ContentType = "application/excel";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        dg.RenderControl(htw);
        Response.Write(sw.ToString());        
        
        Response.End();
        dg = null;
        dg.Dispose();
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
        BindDataGridView();
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = GridView1.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            GridView1.DataSource = dataView;
            GridView1.DataBind();
        }
    }

}